#!/bin/sh

if [ $# -lt 1 ]; then
    echo "Error==>Need args: fxcore[version].[version] [output folder]"
    echo "eg fxcore25581.1599 /home/[username]/Desktop"
    exit 1
fi

SO_FXCORE_VERSION=$1
SO_TARGET_NAME=libPDF2PDF.so
SO_TARGET_OUTPUT_FOLDER_PATH=/usr/lib64
if [ $# -ge 2 ]; then
    SO_TARGET_OUTPUT_FOLDER_PATH=$2
fi

SO_YUM_CONFIG_FILE=./repo/CentOS-Foxit.repo
SO_YUM_ETC_YUM_REPOS_D=/etc/yum.repos.d/
#backup
SO_YUM_ETC_YUM_REPOS_SOURCE=/etc/yum.repos.d/CentOS-Foxit.repo
SO_YUM_ETC_YUM_REPOS_SOURCE_BAK=/etc/yum.repos.d/CentOS-Foxit.repo.bak
#make script as current dir
cd "$(dirname "$0")"
pwd

if [ -f "$SO_YUM_ETC_YUM_REPOS_SOURCE" ]; then 
    echo "Info==>Find original foxit repo config file,backup it: $SO_YUM_ETC_YUM_REPOS_SOURCE"
    mv $SO_YUM_ETC_YUM_REPOS_SOURCE $SO_YUM_ETC_YUM_REPOS_SOURCE_BAK
    if [ $? -ne 0 ]; then
      echo "Backup foxit repo config file failure,remove it now"
      rm -rf $SO_YUM_ETC_YUM_REPOS_SOURCE
    else
      echo "Backup foxit repo config file success"
    fi
fi 

cp -rf $SO_YUM_CONFIG_FILE $SO_YUM_ETC_YUM_REPOS_D
if [ $? -ne 0 ]; then
   echo "Error==>Copy yum config file failure: $SO_YUM_CONFIG_FILE -> $SO_YUM_ETC_YUM_REPOS_D"
   exit 1
else
   echo "Info==>Copy repo config file success: $SO_YUM_CONFIG_FILE -> $SO_YUM_ETC_YUM_REPOS_D"
fi

yum clean all
if [ $? -ne 0 ]; then
   echo "Error==>yum clean all failure"
   exit 1
else
  echo "Info==>yum clean all success"
fi

yum -y install $SO_FXCORE_VERSION
if [ $? -ne 0 ]; then
   echo "Error==>Install fxcore failure"
   exit 1
else
   echo "Info==>Install fxcore success"
fi

# script pwd
echo "current path:"
pwd

# fp_forms pwd
cd ../plugin
echo "current path:"
pwd

make clean
if [ $? -ne 0 ]; then
   echo "Error==>Make clean command failure"
else
   echo "Make clean success"
fi

make
if [ $? -ne 0 ]; then
   echo "Error==>Make command failure"
   exit 1
else
   echo "Make so success"
fi

\cp ./$SO_TARGET_NAME $SO_TARGET_OUTPUT_FOLDER_PATH
if [ $? -ne 0 ]; then
    echo "Error===> copy fail: $SO_TARGET_NAME to $SO_TARGET_OUTPUT_FOLDER_PATH"
else
    echo "Copy success: $SO_TARGET_NAME to $SO_TARGET_OUTPUT_FOLDER_PATH"
fi

make clean
if [ $? -ne 0 ]; then
   echo "Error==>Make clean command failure"
else
   echo "Make clean success"
fi

if [ -f "$SO_YUM_ETC_YUM_REPOS_SOURCE_BAK" ]; then 
   echo "Find backup foxit repo file:$SO_YUM_ETC_YUM_REPOS_SOURCE_BAK,prepare restore it now"
   mv $SO_YUM_ETC_YUM_REPOS_SOURCE_BAK $SO_YUM_ETC_YUM_REPOS_SOURCE
   if [ $? -eq 0 ]; then
      echo "Restore backup foxit repo config file success,$SO_YUM_ETC_YUM_REPOS_SOURCE_BAK -> $SO_YUM_ETC_YUM_REPOS_SOURCE"
   else
      echo "Restore backup foxit repo config file failure,$SO_YUM_ETC_YUM_REPOS_SOURCE_BAK -> $SO_YUM_ETC_YUM_REPOS_SOURCE"
   fi
fi

echo "Remove fxcore from your system"
yum -y erase $SO_FXCORE_VERSION
if [ $? -ne 0 ]; then
   echo "Error==>Remove $SO_FXCORE_VERSION failure"
   exit 1
else
   echo "Info==>Remove $SO_FXCORE_VERSION success"
fi

echo "3Q,Complete all build actions now"




